package Euler407;

import java.util.*;

import ReusableCode.MathFunctions;

public class Idempotents {

	public static void main(String[] args) {
		long sum = 0;
		long upperbound = 20;
		List<Long> pows = new ArrayList<Long>();
		
		for (long n = 0; n <= upperbound; n++)
		{
			pows.add(n*n);
		}
		
		System.out.println("Working...");
		
		for (long n = 1; n <= upperbound; n++)
		{
			List<Long> vals = new ArrayList<Long>();
			long max = Long.MIN_VALUE;
			
			if(MathFunctions.getDivisors(n).size()>3){
				for(long i = 0; i < n; i++)
				{
					long val1 = (pows.get((int)i)%n);
					long val2 = ((int)i%n);
					
					if((val1==val2))
					{
						if((val1>max))
							max = val1;
						
						vals.add(val1);
					}
				}
			}
			else
			{
				max = 1;
			}
			
			//System.out.println(n);
			sum += max;
			System.out.println("n is: " + n + ", max is: " + max + ", and the values are: " + vals);
		}
		
		System.out.println("sum is: " + sum);
	}
}
